import os
import random

import dawdreamer as dmr


def get_files(directory, ext, with_dot=True):
    path_list = []
    for paths in [[os.path.join(dirpath, name).replace('/', '\\') for name in filenames if
                   name.endswith(('.' if with_dot else '') + ext)] for
                  dirpath, dirnames, filenames in os.walk(directory)]:
        path_list.extend(paths)
    return path_list


'''fxp_files = get_files(r'G:\编曲资源', 'fxp')

with open('sylenth1_fxp_files.txt', 'wt', encoding='utf-8') as f:
    f.writelines([file + '\n' for file in fxp_files])'''

with open(r'/reaper_remote/client/sylenth1_fxp_files.txt', 'rt', encoding='utf-8') as f:
    fxp_files = [line.strip() for line in f.readlines()]


engine = dmr.RenderEngine(44100, 256)
instr = engine.make_plugin_processor('instr_1', 'C:/VstPlugins/64bit/Sylenth1.dll')
engine.load_graph([(instr, [])])
preset = random.choice(fxp_files)
instr.load_preset(preset)

track = RPR_GetLastTouchedTrack()
# tr = int(RPR_GetMediaTrackInfo_Value(track, 'IP_TRACKNUMBER'))
_, _, _, fx = RPR_GetFocusedFX2(0, 0, 0)
for i in range(instr.get_plugin_parameter_size()):
    RPR_TrackFX_SetParamNormalized(track, fx, i, instr.get_parameter(i))

RPR_ShowConsoleMsg(RPR_GetResourcePath())
RPR_ShowConsoleMsg(str(fx))
RPR_ShowConsoleMsg(RPR_TrackFX_GetUserPresetFilename(track, fx, preset, 0))
RPR_ShowConsoleMsg(RPR_TrackFX_SetPreset(track, fx, preset))
